package com.horsen.service.aliyun.controller.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.horsen.common.util.R;
import com.horsen.common.util.ResultCodeEnum;
import com.horsen.service.aliyun.service.VideoService;
import com.horsen.service.base.exception.HseduException;
import com.horsen.service.base.util.ExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 文件 控制器
 *
 * @author Horsen
 */
@Api(tags = "阿里云视频点播")
@Slf4j
@RestController("WebVideo")
@RequestMapping("/web/aliyun/vod")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("获得视频播放凭证")
    @GetMapping("/{videoSourceId}")
    public R getPlayAuth(@ApiParam(value = "视频文件ID", required = true)
                         @PathVariable String videoSourceId) {
        try {
            String playAuth = videoService.getPlayAuth(videoSourceId);
            return R.ok("获取视频播放凭证成功").setData("playAuth", playAuth);
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            // 抛出获得播放凭证错误
            throw new HseduException(ResultCodeEnum.FETCH_PLAY_AUTH_ERROR);
        }
    }
}
